﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

using mfLogger.NetworkAppenders;

using mfRCF.Core;
using mfRCF.netmf;

namespace mfLogger.NetduinoPlus2.TestApp
{
    public class Program
    {
        public static ILoggerAppender Logger
        {
            get
            {
                return mfLogger.Instance;
            }
        }

        public static void Main()
        {
            mfNetworkInitialization.InitializeNetworkInfrastructure();

            Utility.SetLocalTime(new DateTime(2013, 8, 4, 23, 55, 00));

            mfLogger.Instance.Name = "MyAppName";
            mfLogger.Instance.MessageCapacity = 20;

            mfLogger.Instance.Appenders.Add(new mfDebugAppender());
            //mfLogger.Instance.Appenders.Add(new mfPeriodicFileAppender());

            StartRcf();

            int warnCounter = 0;
            int errCounter = 0;
            Exception ex = null;

            for (int i = 0; i < int.MaxValue; i++)
            {
                Logger.Info(i + ": TestLog: " + i);

                if ((i % 3) == 0)
                {
                    Logger.Warning(i + ": TestWarn: " + warnCounter);
                    warnCounter++;
                }

                if ((i % 10) == 0)
                {
                    Logger.Error(i + ": TestError: " + errCounter);
                    errCounter++;

                    if ((i % 10) == 0)
                    {
                        ex = new Exception(i + ": TestError: " + errCounter);
                        Logger.Error(ex);
                    }
                }

                //Thread.Sleep(100);
                Thread.Sleep(300);
                //Thread.Sleep(500);
                //Thread.Sleep(750);
                //Thread.Sleep(1000);
            }
        }

        private static void StartRcf()
        {
            //append rss-appender, registers it self on rcf
            mfLogger.Instance.Appenders.Add(new mfRssAppender("/rss"));
            mfLogger.Instance.Appenders.Add(new mfHttpAppender("/log"));

            //start rcf
            mfRcf.Instance.PlattformProvider = new mfRcfPlattformProvider();
            mfRcf.Instance.Start();
        }

    }
}
